package net.dethnull.joscontrol.osc;

import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import net.dethnull.joscontrol.gui.JOSControl;
import oscP5.OscEventListener;
import oscP5.OscMessage;
import oscP5.OscP5;
import oscP5.OscStatus;

/**
 * Project: jOSControl
 * Package: net.dethnull.joscontrol.osc
 * Author: dethnull
 * Date: 2/1/13
 * <p/>
 * <p/>
 * Description:
 */
public class Osc implements OscEventListener, Runnable {

    OscP5 oscP5;

    private int port;


    private boolean hasStarted = false;

    private final ObservableList<Client> clients = FXCollections.observableArrayList();


    public Osc() {}

    public Osc(int port) {
        this.port = port;
    }

    public int getPort() {
        return this.port;
    }

    public void setPort(int port) {
        this.port = port;
    }

    public void hasStarted(boolean has) {
        this.hasStarted = has;
    }

    public boolean getHasStarted() {
        return this.hasStarted;
    }

    public ObservableList<Client> getClients() {
        return this.clients;
    }

    @Override
    public void oscEvent(OscMessage theMessage) {
        String address = theMessage.address();
        String addrPattern = theMessage.addrPattern();
        Object argument = theMessage.arguments()[0];

        if (this.clients.isEmpty()) {
            clients.add(new Client(address, addrPattern, argument));
        } else {
            boolean doesAddrPatternExist = false;

            for (Client clients : this.clients) {
                if (clients.addressPatternProperty().get().equals(theMessage.addrPattern())) {
                    //System.out.println("addrPattern exists, we're not going to re-add it");
                    doesAddrPatternExist = true;
                    boolean doesArgumentExist = false;

                    if (clients.get_arguments().isEmpty()) {
                        clients.set_arguments(argument);
                    } else {
                        for (Object arg : clients.get_arguments()) {
                            if (arg.equals(argument)) {
                                doesArgumentExist = true;

                                if (clients.start()) {
                                    JOSControl.addToDebugLog("thread created");
                                }
                                Double ar = Double.valueOf(argument.toString());
                                if (ar.equals(1.0)) {
                                    clients.sendKeyPress();
                                } else if (ar.equals(0.0)) {
                                    clients.sendKeyRelease();
                                }
                                break;
                            }
                        }
                        if (!doesArgumentExist) {
                            clients.set_arguments(argument);
                        }
                    }
                }
            }
            if (!doesAddrPatternExist) {
                clients.add(new Client(address, addrPattern, argument));
            }

        }
    }

    @Override
    public void oscStatus(OscStatus theStatus) {}

    @Override
    public void run() {
        try {
            hasStarted(true);
            System.out.println("Osc Thread created");
            oscP5 = new OscP5(this, this.getPort());
           // this.setServerIp(oscP5.ip());
            JOSControl.setServerIpText(oscP5.ip());
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }

    public void stop() {
        hasStarted(false);
        for (Client client : this.clients) {
            client.stop();
        }
        oscP5.stop();

    }
}
